package fr.inserm.u915.tabixserver;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

@SuppressWarnings("serial")
public class XSDServlet extends HttpServlet
	{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
		{
		OutputStream out=resp.getOutputStream();
		Build build=(Build)req.getAttribute("build");
		if(build==null) throw new ServletException("build?");
		try
			{
			XMLOutputFactory xmlfactory= XMLOutputFactory.newInstance();
			XMLStreamWriter w= xmlfactory.createXMLStreamWriter(out,resp.getCharacterEncoding());
			w.writeStartDocument(resp.getCharacterEncoding(), "1.0");
			build.writeXsd(w);
			w.writeEndDocument();
			w.flush();
			}
		catch(XMLStreamException err)
			{
			err.printStackTrace();
			throw new IOException(err);
			}
		finally
			{
			out.flush();
			out.close();
			}
		}
	}
